package com.sefer.dragonfly.client.tools;

import java.io.Serializable;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.sefer.dragonfly.client.core.cache.CmCache;
import com.sefer.dragonfly.client.core.cache.CmCacheMuster;
import com.sefer.dragonfly.client.core.container.MonitorContainer;
import com.sefer.dragonfly.client.core.exception.DragonFlyException;

/**
 * 缓存操作使用
 * 
 * @author xiaofeng 2011-11-3 下午03:15:34
 */
public class CmCacheTools {

	private static final Logger logger = Logger.getLogger(CmCacheTools.class);

	/**
	 * 生成key
	 * 
	 * @param plugin
	 * @return
	 */
	public static String generateKey(String pluginId, String pluginVersion) {
		return pluginId + "_" + pluginVersion;
	}

	/**
	 * 获取插件
	 * 
	 * @param plugin
	 * @return
	 */
	public static CmCache<Serializable> getCache(String key) {
		if (StringUtils.isNotBlank(key)) {
			try {
				return MonitorContainer.getContainer().getCache(key);
			} catch (DragonFlyException e) {
				logger.error(e.getMessage(), e);
				return null;
			}
		}
		return null;
	}

	/**
	 * 获取所有插件
	 * 
	 * @param plugin
	 * @return
	 */
	public static CmCacheMuster<String, Serializable> getCacheMuster() {
		try {
			return MonitorContainer.getContainer().getCmCacheMuster();
		} catch (DragonFlyException e) {
			logger.error(e.getMessage(), e);
			return null;
		}
	}
}
